﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using FlowerShop.Core.Models;
using BLL.Services;

namespace 花店管理系统
{
    public partial class Form1 : Form
    {
    
        public Form1()
        {
            
            InitializeComponent();
            
        }
       
        public static int flag = 1;//1为主窗体，2为管理员窗体
        public static bool Aflag = false;//false未登录，true已登录
        public static string Productid = "";

        /*private void Label1_Click(object sender, EventArgs e)
        {

        }*/
        void ShowALLProduct()
        {
            DB.GetCn();
            String str = "select * from Tgood";
            SqlCommand cmd = new SqlCommand(str , DB.cn);
            SqlDataReader rdr = cmd.ExecuteReader();
            while(rdr.Read())
            {
                int index = dgvPriduct.Rows.Add();
                dgvPriduct.Rows[index].Cells[0].Value = rdr[0];
                dgvPriduct.Rows[index].Cells[1].Value = rdr[1];
                dgvPriduct.Rows[index].Cells[2].Value = rdr[3];
                dgvPriduct.Rows[index].Cells[3].Value = rdr[4];
                dgvPriduct.Rows[index].Cells[4].Value = rdr[5];
                dgvPriduct.Rows[index].Cells[5].Value = rdr[6];
                dgvPriduct.Rows[index].Cells[6].Value = rdr[9];
                dgvPriduct.Rows[index].Cells[7].Value = rdr[10];

                try
                {
                    Image imageColum = Image.FromFile(Application.StartupPath + rdr[11]);
                    dgvPriduct.Rows[index].Cells["Column9"].Value = imageColum;
                }
                catch
                {
                    Image imageColum = Image.FromFile(Application.StartupPath + "\\" + "暂无图片.png");
                    dgvPriduct.Rows[index].Cells["Column9"].Value = imageColum;
                }
            }
            rdr.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dgvPriduct.RowTemplate.Height = 90;
            dgvPriduct.Columns[0].Width = 80;
            dgvPriduct.Columns[1].Width = 80;
            dgvPriduct.Columns[2].Width = 80;
            dgvPriduct.Columns[3].Width = 70;
            dgvPriduct.Columns[4].Width = 60;
            dgvPriduct.Columns[5].Width = 80;
            dgvPriduct.Columns[6].Width = 100;
            dgvPriduct.Columns[7].Width = 100;
            dgvPriduct.Columns[8].Width = 100;
            dgvPriduct.Columns[9].Width = 100;
            dgvPriduct.Columns[10].Width = 100;
            label5.Text = Login.username;
            ShowALLProduct();
        }

        private void Label1_Click(object sender, EventArgs e)
        {

        }

        private void BtnShowAll_Click(object sender, EventArgs e)
        {
            dgvPriduct.Rows.Clear();
            ShowALLProduct();
        }

        private void BtnSearch1_Click(object sender, EventArgs e)//按商品名称关键字查询
        {
            if(txtPName.Text=="")
            {
                MessageBox.Show("请输入查询关键字");

            }
            else
            {
                dgvPriduct.Rows.Clear();
                DB.GetCn();
                string str = "select * from Tgood where G_name like '%" + txtPName.Text + "%'";
                SqlCommand cmd = new SqlCommand(str, DB.cn);
                SqlDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    int index = dgvPriduct.Rows.Add();
                    dgvPriduct.Rows[index].Cells[0].Value = rdr[0];
                    dgvPriduct.Rows[index].Cells[1].Value = rdr[1];
                    dgvPriduct.Rows[index].Cells[2].Value = rdr[3];
                    dgvPriduct.Rows[index].Cells[3].Value = rdr[4];
                    dgvPriduct.Rows[index].Cells[4].Value = rdr[5];
                    dgvPriduct.Rows[index].Cells[5].Value = rdr[6];
                    dgvPriduct.Rows[index].Cells[6].Value = rdr[9];
                    dgvPriduct.Rows[index].Cells[7].Value = rdr[10];

                    try
                    {
                        Image imageColum = Image.FromFile(Application.StartupPath + rdr[11]);
                        dgvPriduct.Rows[index].Cells["Column9"].Value = imageColum;
                    }
                    catch
                    {
                        Image imageColum = Image.FromFile(Application.StartupPath + "\\" + "暂无图片.png");
                        dgvPriduct.Rows[index].Cells["Column9"].Value = imageColum;
                    }
                }
                rdr.Close();
            }
        }

        private void TxtPName_TextChanged(object sender, EventArgs e)
        {

        }

        private void BtnSearch2_Click(object sender, EventArgs e)//按商品价格区间查询
        {
            dgvPriduct.Rows.Clear();
            string str = "";
            DB.GetCn();
            int i = comboBox1.SelectedIndex;
            switch(i)
            {
                case 0:
                    str = "G_xiaoshoujia<=10";
                    break;

                case 1:
                    str= "G_xiaoshoujia>10 and G_xiaoshoujia<=50";
                    break;
                case 2:
                    str = "G_xiaoshoujia>50 and G_xiaoshoujia <=100";
                    break;
                case 3:
                    str = "G_xiaoshoujia>100 and G_xiaoshoujia <=200";
                    break;
                default:
                    str = "G_xiaoshoujia>200";
                    break;
            }
            string sdr = "select * from Tgood where " + str + "";
            SqlCommand cmd = new SqlCommand(sdr, DB.cn);
            SqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                int index = dgvPriduct.Rows.Add();
                dgvPriduct.Rows[index].Cells[0].Value = rdr[0]; 
                dgvPriduct.Rows[index].Cells[1].Value = rdr[1];
                dgvPriduct.Rows[index].Cells[2].Value = rdr[3];
                dgvPriduct.Rows[index].Cells[3].Value = rdr[4];
                dgvPriduct.Rows[index].Cells[4].Value = rdr[5];
                dgvPriduct.Rows[index].Cells[5].Value = rdr[6];
                dgvPriduct.Rows[index].Cells[6].Value = rdr[9];
                dgvPriduct.Rows[index].Cells[7].Value = rdr[10];

                try
                {
                    Image imageColum = Image.FromFile(Application.StartupPath + rdr[11]);
                    dgvPriduct.Rows[index].Cells["Column9"].Value = imageColum;
                }
                catch
                {
                    Image imageColum = Image.FromFile(Application.StartupPath + "\\" + "暂无图片.png");
                    dgvPriduct.Rows[index].Cells["Column9"].Value = imageColum;
                }
            }
            rdr.Close();
        }

        private void BtnLogin_Click(object sender, EventArgs e)
        {
            this.Hide();
            Login ll = new Login();
            ll.ShowDialog();
            if(flag==1)
            {
                
                this.Visible = true;
                label5.Text = Login.username;

            }
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void btnresgter_Click(object sender, EventArgs e)
        {
            Register rl = new Register();
            rl.ShowDialog();
      
        }

        private void dgvPriduct_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int CIndex = e.ColumnIndex;
            //如果是第9列，查看详细信息
            if(CIndex==9)
            {
                //确定是第几行
                int rowindex = e.RowIndex;
                Productid = dgvPriduct.Rows[rowindex].Cells[0].Value.ToString();
                Tproducts tl = new Tproducts();
                tl.ShowDialog();
            }
            //如果是第10列，查看购物车
            if(CIndex==10)
            {
                if(Aflag==false)
                {
                    MessageBox.Show("请先登录");
                    Login ll = new Login();
                    ll.ShowDialog();
                }
                else
                {
                    //确定是第几行
                    int rowidex = e.RowIndex;
                    Productid = dgvPriduct.Rows[rowidex].Cells[0].Value.ToString();
                    if (Convert.ToInt32(dgvPriduct.Rows[rowidex].Cells[5].Value) < 1)
                    {
                        MessageBox.Show("该商品暂时缺货");
                    }
                    else
                    {
                        ShopCart sl = new ShopCart();
                        sl.ShowDialog();
                    }
                }    
                
            }
        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (Aflag == false)
            {
                MessageBox.Show("请先登录");
                Login ll = new Login();
                ll.ShowDialog();
            }
            else
            {
                Customer_Services.Client client = new Customer_Services.Client();
                client.Show();
            }
        }

        private void btnGeren_Click(object sender, EventArgs e)
        {
            if (Aflag == false)
            {
                MessageBox.Show("请先登录");
                Login ll = new Login();
                ll.ShowDialog();
            }
            else
            {
                geren.Shouye sl = new geren.Shouye();
                sl.ShowDialog();
            }
           
        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
